﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Configuration;
namespace DAL
{
    /// <summary>
    /// 人员表
    /// </summary>
    public class UsersDal : BaseDal<SysUser>
    {
        public override string tablename
        {
            get { return "SysUser"; }
        }

        public override Dictionary<string, bool> PKName
        {
            get
            {
                Dictionary<string, bool> dic = new Dictionary<string, bool>();
                dic.Add("users_Id", true);
                return dic;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UsersName"></param>
        /// <param name="UsersPwd"></param>
        /// <returns></returns>
        public List<SysUser> UsersLogin(string UsersName, string UsersPwd)
        {
            using(SqlConnection conn =new SqlConnection (ConfigurationManager.ConnectionStrings["db"].ConnectionString))
            {
                string sql = "select * from  sysuser a join sysroles b on a.roles_id=b.roles_id where Users_Account='"+UsersName+"' and Users_Pwd='"+UsersPwd+"' and Roles_IsState=0";
            DataTable dt = Common.DBHelp.GetDataSet(sql); 


            List<SysUser> user = new List<SysUser>();
            foreach (DataRow item in dt.Rows)
            {
                SysUser user1 = new SysUser();
                foreach (var i in user1.GetType().GetProperties())
                {
                    i.SetValue(user1, item[i.Name]);
                }
                user.Add(user1);
            }
            return user;
            }

        }

        
       /// <summary>
       ///  根据部门编号查询改部门编号下的勘察员
       /// </summary>
       /// <param name="value"></param>
       /// <returns></returns>
        public List<SysUser> GetUsers(int newValue, int role)
        {
            SqlParameter[]
                Parameter = new SqlParameter[]{
                    new SqlParameter("@Department_Id",newValue),
                    new SqlParameter("@Roles_id",role)
                };
            string Sql = Common.Common.GetTable<SysUser>(tablename, Parameter);
            DataTable dt = Common.DBHelp.GetDataSet(Sql);


            List<SysUser> user = new List<SysUser>();
            foreach (DataRow item in dt.Rows)
            {
                SysUser user1 = new SysUser();
                foreach (var i in user1.GetType().GetProperties())
                {
                    i.SetValue(user1, item[i.Name]);
                }
                user.Add(user1);
            }
            return user;

        }
        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="Users_Name">用户名</param>
        /// <param name="Users_IsState">状态编号</param>
        /// <returns></returns>
        public bool updateState(string Users_Name,int Users_IsState )
        {
            string sql = "update SysUser set Users_IsState=" + Users_IsState + " where Users_Name='" + Users_Name + "'";
            return Common.DBHelp.GetExecuteNonQuery(sql);
        }
        public bool AddUser(SysUser u)
        {
            string sql = @"insert into sysuser ([Users_Name],[Users_Account],[Users_Pwd],[Users_TEL],[Roles_id],[Department_Id],[Users_IsState],[Users_CreateTime],[Users_CreatePeople]) values('"+u.users_Name+"','"+u.users_Account+"','"+u.users_Pwd+"','"+u.users_TEL+"','"+u.Roles_id+"','"+u.Department_Id+"','"+u.users_IsState+"','"+u.users_CreateTime+"','"+u.users_CreatePeople+"')";
            return Common.DBHelp.GetExecuteNonQuery(sql);
        }
            
    }
}
